
load("js/cib_mobile/Barrett.js")
load("js/cib_mobile/BigInt.js")
load("js/cib_mobile/RSA.js")

var currecy = "";// 信用卡的币种

var date ="";//信用卡的账单日
var open_time = "";//借记卡的开卡时间
var currency_type = "";//借记卡的币种
var  depoitSdf = new SimpleDateFormat("yyyy-MM-dd");//借记卡
var  creditSdf = new SimpleDateFormat("YYYY-MM");//信用卡
var urls = {
    USER_AGENT : "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Mobile Safari/537.36",
    ACCEPT:"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"

}
var creditList = new ArrayList();

var v6_cib_mobile_credit_java_funcs ={
    checkProxyAvailable : function(){ return true },
    checkTargetWebsiteAvailable : function(){ return true },
    checkControlAvailable : function () { return true },
    login:function(){
        var cardNo = getLoginParams(context)[0];//卡号
        var certNo = getLoginParams(context)[1];//身份证
        var passWord = context.get(Constants.LUA_TASK_TOKEN);

        RequestUtil.get("https://3g.cib.com.cn/app/00002.html");


        //请求公钥
        var param = new HashMap();
        param.put("menu", "0100000020");
        var indexHtml = RequestUtil.post("https://3g.cib.com.cn/app/80820.html", param, getHttpPostTwo());
        storeDataToFile(context, indexHtml, "indexHtml.html");
        var first = indexHtml.indexOf("publicKey = ");
        var publicKeyStr = indexHtml.substring(first + "publicKey = ".length, first + "publicKey = ".length + 258);
        publicKeyStr = publicKeyStr.replaceAll("\"", "").replaceAll("\"", "");
        if(publicKeyStr.length() !== 256){
            publicKeyStr = "";
        }
        logger.info("任务【 {} 】公钥【 {} 】",context.get(LUA_TASK_KEY), publicKeyStr);

        //加密密码
        var passWordEncrypt = hsRSAEncrypt(passWord,publicKeyStr);
        logger.info("任务【 {} 】密文【 {} 】",context.get(LUA_TASK_KEY), passWordEncrypt);


        param.clear();
        //登录并返回结果
        var secendUrl = "https://3g.cib.com.cn/app/00002.html";
        param.put("flowsn", "402");
        param.put("hsm_pwd", passWordEncrypt);
        param.put("select_flag", "0");
        param.put("loginname", cardNo);
        param.put("req_zjhm", certNo);
        param.put("pwd", passWordEncrypt);
        var loginResultHtml = RequestUtil.post(secendUrl,param,getHttpPostTwo());
        storeDataToFile(context, loginResultHtml, "loginResult.html");

        var isLoginCorrectFunc = function(loginResultHtml) {
            if(StringUtils.isBlank(loginResultHtml)) {
                return false;
            }
            var doc = Jsoup.parse(loginResultHtml);

            if(equals(doc.select("#cvv2").attr("placeholder"), "卡片签名栏末三位")){
                return false

            }else if(equals("请按以下流程激活信用卡：",doc.select(".words").text())){
                return false
            }

            var checkText = doc.select("h2.mk-title").text()
            logger.info("任务【 {} 】待检查文本: '{}'", context.get(LUA_TASK_KEY), checkText)
            return StringUtils.isNotBlank(checkText) && checkText.contains("尊敬的")
        }

        var getLoginErrorMsgFunc = function(loginResultHtml) {
            if(StringUtils.isBlank(loginResultHtml)) {
                return "登录超时";
            }
            var doc = Jsoup.parse(loginResultHtml);

            var checkText = doc.select("#cvv2").attr("placeholder")
            logger.info("任务【 {} 】待检查文本: '{}'", context.get(LUA_TASK_KEY), checkText)

            var  activateCard = doc.select(".words").text();
            logger.info("任务【 {} 】激活的卡: '{}'", context.get(LUA_TASK_KEY), activateCard)

            if(equals(checkText, "请输入签名栏末三位")){
                return "请输入卡片签名栏末三位"
            }else if(equals("请按以下流程激活信用卡：",activateCard)){
                return "请按以下流程激活信用卡"
            }

            var errorMsg = doc.select("p.result-describe").text();
            logger.info("任务【 {} 】错误信息: {}", context.get(LUA_TASK_KEY), errorMsg)
            if(errorMsg.contains("未配置错误信息")){
                return "未配置错误信息"
            }
            return errorMsg
        }

        assertTrueOrThrowException(loginResultHtml, isLoginCorrectFunc, getLoginErrorMsgFunc)
        logger.info("任务【 {} 】登录成功", context.get(LUA_TASK_KEY))
    },
    crawl :function() {
        var cardNum = 1;
        if ( getLoginParams(context)[0].length() == 16) {// 信用卡
            logger.info("{},信用卡开始",context.get(Constants.LUA_TASK_KEY));


            //向上下文中设置信用卡数量
            context.put("creditCardSize",1);

            //构建表头
            createData();

            //获取构建到的表头信息
            var dataList = context.get(ALL_DATA);

            //因为兴业的卡是一张卡，所以只有一个对象
            var creditCardAllData = dataList.get(0);

            //  ###########################【获取账单日=========解析账单日】################
               getDateHtml();
              date =  parseDateHtml();
             //如果账单日为空，那我们就给他个默认值
             date = equals(date,"") ? "1" : date;
            logger.info("{},账单日:{}",context.get(Constants.LUA_TASK_KEY),date);

            //  ##########################【获取基本信息页面=====解析基本页面】 ####################
            getBasicNeedInfoHtml(cardNum);
            parseBasicNeedInfo(creditCardAllData);

            //############################【获取已出账单页面======解析已出账单页面】#################

                  //获取六个月的集合
                 var sixMons = getBankTimeList(date);
                 //翻转集合   目的是为了解决带有上期的字段的值
                 Collections.reverse(sixMons);

            for(var i = 0;i < sixMons.size(); i ++){
                var everyMonth = sixMons.get(i);
                var billEveryMonth = everyMonth;
                logger.info("{},6个月中的每一个月:{}",context.get(Constants.LUA_TASK_KEY), everyMonth);

                var installments = new ArrayList();
                var shopping_sheets = new ArrayList();

                //初始化billAllData
                var billAllData = getDoneBillAllDataHeaderData(creditCardAllData);
                    everyMonth = everyMonth.replaceAll("-", "");

                //################ 【获取每个月账单的最低还款===本期应还金额.... 并 解析每个月账单的最低还款===本期应还金额...】 ##################
                 getEveryMonthBillTotalMsg(everyMonth,cardNum);
                 parseEveryMonthBillTotalMsg(billAllData,everyMonth);

                 //##################【用于 计算每个月的金额】##########
                    var moneySum = 0;//new_charges
                    var pay = 0;//last_payment

                 //##############【获取每个月账单第一页的消费信息 并 解析每个月账单第一页的消费信息】#####################
                 getEveryMonthBillFirstPageHtml(everyMonth,cardNum);
                 parseEveryMonthBillFirstPageHtml(everyMonth,cardNum,billAllData,shopping_sheets,installments,moneySum,pay,creditCardAllData);

                 //##################【从第一页获取index的数量】##################
                  var index = context.get("billIndexCount");
                  if(index != null){
                      index =  Integer.parseInt(index);
                  }
                  logger.info("{}，{}月第一页总共【{}】条",context.get(Constants.LUA_TASK_KEY),everyMonth,index==null ? "没有消费明细":index);

                 //##################【从第一页获取总页数】#############
                    var  totalPages =  context.get("totalPages");
                    var sumPage = null;
                    if (StringUtils.isNotBlank(totalPages)) {
                        sumPage = totalPages.split("/");
                    }
                  logger.info("{}，{}月已出账单总共【{}】页",context.get(Constants.LUA_TASK_KEY),everyMonth,Integer.parseInt(sumPage == null ? 1:sumPage[1].replaceAll(" ", "")));

                 //##############【获取每个月账单第二页以上的消费信息   并 解析每个月账单第二页以上的消费信息】###############
                    if(sumPage != null ){
                        for (var j = 2; j <= Integer.parseInt(sumPage[1].replaceAll(" ", "")); j ++) {
                            getEveryMonthBillMorePageHtml(everyMonth,j,cardNum);
                            parseEveryMonthBillMorePageHtml(everyMonth,j,cardNum,creditCardAllData,index,billAllData,shopping_sheets);
                        }
                    }

                    //#####################【处理已出账单的最终结果】#################

                    handleEveryMonthBillResult(billAllData,billEveryMonth,creditCardAllData,i);
            }


            //####################【未出账单】################

                    //获取一个干净的未出账单billAllData
                    var billAllDataWithOut = getUNDoneBillAllDataHeaderData(creditCardAllData);

                       //------------【用于计算未出账单的金额】-----------
                            var lastPayment = 0.0 ;
                            var newCharges = 0.0;
                            var newBalance = 0.0;


                    //**************************【获取未出账单的第一页 并解析 未出账单的第一页】****************************
                      var shoppingSheetsUndone = new ArrayList();
                       getUndoneFirstHtml(cardNum);
                       parseUndoneFirstHtml(shoppingSheetsUndone,creditCardAllData,billAllDataWithOut,lastPayment,newCharges,newBalance);


                       //###################【未出账单第一页的条数】##############
                       var indexUndone = context.get("index");
                       if(indexUndone != null){
                           indexUndone =  Integer.parseInt(indexUndone);
                       }
                        logger.info("{},未出账单第一页的总【{}】条数",context.get(Constants.LUA_TASK_KEY),indexUndone==null ?"没有消费明细":indexUndone);

                       //**********************【未出账单总页数】*************
                      var  undoneTotalPages =  context.get("undoneTotalPages");
                       var  undoneSumPage = null;
                       if(StringUtils.isNotBlank(undoneSumPage)){
                           undoneSumPage =  getFen(undoneTotalPages).size();
                           logger.info("{},未出账单总页数:{}",context.get(Constants.LUA_TASK_KEY),undoneSumPage);
                       }

                       //<><><><><><><><><><><><>【获取未出账单的第二页以上的页数】<><><><><><><><><><><><><>
                        if(undoneSumPage != null){
                            for (var currentPage = 2; currentPage <= undoneSumPage; currentPage ++) {

                                getUndoneMorePagesHtml(cardNum,currentPage);
                                parseUndoneMorePagesHtml(currentPage,billAllDataWithOut,shopping_sheets,creditCardAllData,indexUndone);

                            }
                        }


                        //**********************【处理未出账单】**********************
                          handleUndoneBill(billAllDataWithOut,creditCardAllData);

        } else {// 借记卡
            logger.info("{},借记卡开始",context.get(Constants.LUA_TASK_KEY));

            //向上下文中设置信用卡数量
            context.put("debitCardSize",1);

            //构建表头
            createData();

            //获取构建到的表头信息
            var dataList = context.get(ALL_DATA);

            //因为兴业的卡是一张卡，所以只有一个对象
            var creditCardAllData = dataList.get(0);

            //************************ 【获取 余额 并解析】********************************
                        getBalanceHtml()
                        parseBalanceHtml(creditCardAllData)

            //@@@@@@@@@@@@@@@@@@@@@@@@@@【获取开卡时间页面  并解析】@@@@@@@@@@@@@@@@@@@@@@@@
                    getOpenTimeHtml()
                    parseOpenTimeHtml(creditCardAllData)

            //========================【获取卡号、持卡人、开户行页面  并 解析 】==========================
                    getBaseInfoHtml()
                    parseBaseInfoHtml(creditCardAllData)

            //===========================【账单流水】================================

            //--------------【获取每个月的第一天和最后一天，即使当月没有到月底，返回当前的日期】
                    var monthFirstDay = getMonthFirstDay();
                    var monthLastDay = getMonthLastDay();

                    for(var i = 0; i < 12; i ++){

                        //如果在开卡时间之前  就删除开卡时间之前的月份
                        if(StringUtils.isNotBlank(open_time)){
                            if (sdf.parse(open_time).after(sdf.parse(monthFirstDay.get(i)))
                                && sdf.parse(open_time).after(sdf.parse(monthLastDay.get(i)))) {
                                break;
                            }
                        }


                        //每月的第一天
                        var firstDay = monthFirstDay.get(i).replaceAll("-", "");
                        //每月的最后一天
                        var lastDay = monthLastDay.get(i).replaceAll("-", "");

                        //初始化billAllData
                        var billAllData =  getDoneBillAllDataHeaderData(creditCardAllData);
                        billAllData.setShoppingSheets(new ArrayList());

                        //【获取 每个月账单的总信息 包括总页数、第一页的消费的总条数 并解析】
                                getBillEveryMonthFirstHtml(cardNum,firstDay,lastDay)
                                parseBillEveryMonthFirstHtml(firstDay)

                        //【每个月账单的总页数】
                        var sumPage =  context.get("totalPages");
                        var sumP = null;
                        if(StringUtils.isNotBlank(sumPage)){
                            sumP = sumPage.split("/");
                        }
                        logger.info("{},{}月账单的总页数:【{}】",context.get(Constants.LUA_TASK_KEY),firstDay,null == sumP ? "没有消费明细": Integer.parseInt(sumP[1]));

                        //【每个月的账单的总消息的页面 显示的消费信息的条数】
                        var children =  context.get("firstPageExpenseItem");
                        logger.info("{},{}月账单的第一页显示的条数：【{}】",context.get(Constants.LUA_TASK_KEY),firstDay,null == children ? "消费条数是0":children.size()-2);


                        //【获取每个月第一页的 每笔消费信息 并解析】
                        getEveryMonthEveryExpenseHtml(cardNum,children,firstDay)
                        parseEveryMonthEveryExpenseHtml(cardNum,children,firstDay,billAllData,creditCardAllData)

                        //【获取每个月账单的 第二页及更多的页数 并解析】
                        if(null != sumP){
                            //分页
                            for(var currentPage = 2 ; currentPage <=Integer.parseInt(sumP[1]); currentPage ++) {

                                getEveryMonthBillMorePageSumItemHtml(cardNum,firstDay,currentPage)
                                parseEveryMonthBillMorePageSumItemHtml(cardNum,firstDay,currentPage)

                                var  childs =  context.get("morePageSumItem");
                                logger.info("{},{}月{}页，总【{}】条数",context.get(Constants.LUA_TASK_KEY),firstDay,currentPage,null == childs ? "没有消费明细":childs.size()-2);

                                getEveryMonthBillMorePageExpenseHtml(cardNum,firstDay,currentPage,childs)
                                parseEveryMonthBillMorePageExpenseHtml(firstDay,currentPage,childs,billAllData,creditCardAllData)
                            }
                        }

                        //处理账单
                        handlerEveryMonth(monthFirstDay.get(i),billAllData)

                    }


            //()()()()()()()()()()()【定期 活期】()()()()()()()()()()()
                    getDepositHtml()
                    parseDepositHtml(creditCardAllData)

        }

    },
    exitLogin :function () {}
}

/**
 * 给pwd加密
 * @type {string}
 */
function hsRSAEncrypt(str,publicKey){
    if(publicKey=="") return str;
    setMaxDigits(130);
    var key = new RSAKeyPair("10001" , "" , publicKey);
    var enStr = encryptedString(key,str);
    return enStr;
}

/**
 * 【获取信用卡的账户信息的页面】
 *
 * @param creditCardAllData
 * @throws Exception
 */
function  getBasicNeedInfoHtml(cardNum)  {
    logger.info("{},开始获取信用卡账户信息页面",context.get(Constants.LUA_TASK_KEY));
        var i = 0;
       var secendUrl = "https://3g.cib.com.cn/app/80820.html";
        var param = new HashMap();
        param.put("flowsn", "50");
        param.put("secmenu", "0200000394");

        do{
            var baseInfoHtml = RequestUtil.post(secendUrl,param,getHttpPostCreditOne());
            i ++;
        }while(!baseInfoHtml.contains("账户信息") && i < 3);

         storeDataToFile(context,baseInfoHtml, cardNum + "_card.html");
        context.put("baseInfoHtml",baseInfoHtml);
}


/**
 *  【解析信用卡账户信息页面】
 */
function  parseBasicNeedInfo(creditCardAllData){
    logger.info("{},开始解析信用卡账户信息页面",context.get(Constants.LUA_TASK_KEY));

    var baseInfoHtml =  context.get("baseInfoHtml");
    if(baseInfoHtml.contains("账户信息") && StringUtils.isNotBlank(baseInfoHtml)){

        try{
            var doc = Jsoup.parse(baseInfoHtml);
            var table = doc.getElementsByClass("MBcontent1").get(0);
            var trs = table.select("tr");
            // 用户名
            var userName = "";
            // 可用信用额度
            var availableLimit = "";
            // 信用额度
            var creditLimit = "";
            // 可用取现额度
            var enChashMentImprest = "";
            // 取现额度
            var cashLimit = "";
            // 卡号
            var fullCardNum = doc.select("#rmbxx > table > tbody > tr:nth-child(2) > td:nth-child(3)").text().trim().replaceAll("\\s*","");
            if (trs.size() == 30) {// li
                // 用户名
                userName = doc.select("#rmbxx > table > tbody > tr:nth-child(3) > td:nth-child(3)").get(0).text();
                // 可用信用额度
                availableLimit = doc.select("#rmbxx > table > tbody > tr:nth-child(8) > td:nth-child(3)").get(0).text();
                // 信用额度
                creditLimit = doc.select("#rmbxx > table > tbody > tr:nth-child(7) > td:nth-child(3)").get(0).text();
                // 可用取现额度
                enChashMentImprest = doc.select("#rmbxx > table > tbody > tr:nth-child(10) > td:nth-child(3)").get(0)
                    .text();
                // 取现额度
                cashLimit = doc.select("#rmbxx > table > tbody > tr:nth-child(9) > td:nth-child(3)").get(0).text();
            } else if (trs.size() == 31) {
                // 用户名
                userName = doc.select("#rmbxx > table > tbody > tr:nth-child(4) > td:nth-child(3)").get(0).text();
                // 可用信用额度
                availableLimit = doc.select("#rmbxx > table > tbody > tr:nth-child(9) > td:nth-child(3)").get(0).text();
                // 信用额度
                creditLimit = doc.select("#rmbxx > table > tbody > tr:nth-child(8) > td:nth-child(3)").get(0).text();
                // 可用取现额度
                enChashMentImprest = doc.select("#rmbxx > table > tbody > tr:nth-child(11) > td:nth-child(3)").get(0)
                    .text();
                // 取现额度
                cashLimit = doc.select("#rmbxx > table > tbody > tr:nth-child(10) > td:nth-child(3)").get(0).text();
            }

            creditCardAllData.setFullCardNum(fullCardNum);// 卡号
            creditCardAllData.setCardNum(fullCardNum.substring((fullCardNum.length() - 4), (fullCardNum.length())));// 卡号后四位
            creditCardAllData.setNameOnCard(userName);
            creditCardAllData.setCreditLimit(new BigDecimal(creditLimit));
            creditCardAllData.setBalance(new BigDecimal(availableLimit));
            creditCardAllData.setCashBalance(new BigDecimal(enChashMentImprest));
            creditCardAllData.setCashLimit(new BigDecimal(cashLimit));
            creditCardAllData.setOpenBank("");
        } catch (e) {
            logger.info("{},信用卡账户信息解析异常：{}",context.get(Constants.LUA_TASK_KEY),e);
            statCaughtExceptionByJS(context, e);
        }
    }
}


/**
 * 【获取每个月的账单的总的信息 如：最低还款、本月应还金额】
 */
function getEveryMonthBillTotalMsg(everyMonth,cardNum){
    logger.info("{},获取{}已出账单的汇总信息",context.get(Constants.LUA_TASK_KEY), everyMonth);
      var i = 0;
     var param = new HashMap();
        param.put("bbxx", "01");
        param.put("h2", "submiturl=50002_1|flowsn=-100");
        param.put("h3", "submiturl=50003_1|flowsn=-100");
        param.put("querydate", everyMonth);
        param.put("mainflag", "1");

    var fiveUrl = "https://3g.cib.com.cn/app/50002.html";
    do{
        var everyMonthTotalMsgHtml =RequestUtil.post(fiveUrl,param,getHttpPostCreditTwo());
         i ++;
    }while(!everyMonthTotalMsgHtml.contains("人民币账单") && i < 5);

    storeDataToFile(context,everyMonthTotalMsgHtml, cardNum + "_bill_" + everyMonth + "Mon.html");





    if (everyMonthTotalMsgHtml.contains("错误提示页面")) {
        return;
    }

    context.put("everyMonthTotalMsgHtml",everyMonthTotalMsgHtml);

}


/**
 * 【解析每个月的账单的总的信息 如：最低还款、本月应还金额】
 */
function parseEveryMonthBillTotalMsg(billAllData,everyMonth){
    logger.info("{},解析【{}】已出账单的汇总信息",context.get(Constants.LUA_TASK_KEY), everyMonth);

    var  everyMonthTotalMsgHtml =  context.get("everyMonthTotalMsgHtml");
    try{
        if( StringUtils.isNotBlank(everyMonthTotalMsgHtml) && everyMonthTotalMsgHtml.contains("人民币账单")){

            var doc = Jsoup.parse(everyMonthTotalMsgHtml);
            var tablem = doc.select("table");
            if (tablem.size() == 2 || tablem.size() == 0){
                return;
            }

            var table = doc.select("table.MBcontent1:nth-child(3)");
            var trs = null ;
            if(table !==  null){
                trs = table.select("tr");
            }
            // 最低还款
            var minRepayment = "";
            // 还款日
            var repaymentDate = "";
            // 本期应还总额
            var sumBalance = "";
            var moneySum = 0;
            var pay = 0;

            if (trs.size() == 13) {// han

                // 最低还款
                minRepayment = doc
                    .select("table.MBcontent1:nth-child(3) > tbody:nth-child(1) > tr:nth-child(8) > td:nth-child(2)")
                    .text().trim();
                // 还款日
                repaymentDate = doc
                    .select("table.MBcontent1:nth-child(3) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(2)")
                    .text().trim();
                // 本期应还总额
                sumBalance = doc
                    .select("table.MBcontent1:nth-child(3) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(2)")
                    .text().trim();
                // 币种------------账单详情页面需要
                currecy = doc
                    .select("table.MBcontent1:nth-child(3) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(2)")
                    .text().trim();
            } else if (trs.size() == 12) {// li

                // 最低还款
                minRepayment = doc
                    .select("body > div:nth-child(3) > table:nth-child(3) > tbody > tr:nth-child(7) > td:nth-child(2)")
                    .text().trim();
                // 还款日
                repaymentDate = doc
                    .select("body > div:nth-child(3) > table:nth-child(3) > tbody > tr:nth-child(4) > td:nth-child(2)")
                    .text().trim();
                // 本期应还总额
                sumBalance = doc
                    .select("body > div:nth-child(3) > table:nth-child(3) > tbody > tr:nth-child(6) > td:nth-child(2)")
                    .text().trim();
                // 币种------------账单详情页面需要
                currecy = doc
                    .select("body > div:nth-child(3) > table:nth-child(3) > tbody > tr:nth-child(5) > td:nth-child(2)")
                    .text().trim();
            }

            billAllData.setPaymentDueDate(
                yyyy_MM_dd_PARSER.parse(processDate(repaymentDate).replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-")));

            billAllData.setMinPayment(new BigDecimal(minRepayment));
            context.put("sumBalance", sumBalance);
            billAllData.setNewBalance(new BigDecimal(String.format("%.2f", Double.parseDouble(sumBalance))));

        }
    }catch (e) {
        logger.info("{},解析【{}】已出账单的汇总信息异常：{}",context.get(Constants.LUA_TASK_KEY), everyMonth,e);
        statCaughtExceptionByJS(context, e);
    }
}


/**
 * 【获取每个月账单的第一页消费信息的页面】
 */
function getEveryMonthBillFirstPageHtml(everyMonth, cardNum){
        logger.info("{},获取【{}】月的账单的第一页的消费信息的页面:",context.get(Constants.LUA_TASK_KEY),everyMonth);

        var i = 0;
        var param = new HashMap();
            param.put("bbxx", "01");
            param.put("h2", "submiturl=50002_1|flowsn=-100");
            param.put("h3", "submiturl=50003_1|flowsn=-100");
            param.put("querydate", everyMonth);
            param.put("mainflag", "1");
        var sixUrl = "https://3g.cib.com.cn/app/50003.html";

        do{
            var everyMonthBillShoppingSheetsHtml = RequestUtil.post(sixUrl,param,getHttpPostCreditTwo());
            i ++;
        }while(!everyMonthBillShoppingSheetsHtml.contains("人民币账单明细") &&  i < 5);
        storeDataToFile(context,everyMonthBillShoppingSheetsHtml, cardNum + "_bill_shopping_sheets_" + everyMonth + "Mon_1Page" + ".html");

        if (everyMonthBillShoppingSheetsHtml.contains("错误提示页面")) {
                return;
        }

        context.put("everyMonthBillShoppingSheetsHtml",everyMonthBillShoppingSheetsHtml);

}


/**
 * 【解析每个月的账单的第一页的消费信息】
 */
function parseEveryMonthBillFirstPageHtml(everyMonth, cardNum, billAllData, shopping_sheets, installments,moneySum,pay,creditCardAllData){
    logger.info("{},解析【{}】月的账单的第一页的消费信息的页面",context.get(Constants.LUA_TASK_KEY),everyMonth);

   var  everyMonthBillShoppingSheetsHtml = context.get("everyMonthBillShoppingSheetsHtml");

   try{
       if( StringUtils.isNotBlank(everyMonthBillShoppingSheetsHtml) && everyMonthBillShoppingSheetsHtml.contains("人民币账单明细")){
           billAllData.setInstallments(installments);

           var docs = Jsoup.parse(everyMonthBillShoppingSheetsHtml);
           // 总页数
           var sumPages = docs
               .select("body > div:nth-child(3) > table:nth-child(5) > tbody > tr:nth-child(14) > td:nth-child(2) > form")
               .text().replaceAll("下一页", "").replaceAll("\\[", "").replaceAll("\\]", "");
           context.put("totalPages",sumPages);

           var tables = docs.select("table");

           var index = 0;
           var ins;

           if (tables.size() > 2) {
               var table2 = tables.get(3);
               var mxtrs = table2.select("tr");

               for (var i = 3; i < mxtrs.size() - 6; i++) {
                   var expense = new Expense();
                   var tds = mxtrs.get(i).children();
                   var jyr = tds.get(1).text().trim();
                   var  je = tds.get(2).text().trim();
                   var zy = tds.get(3).text().trim().replaceAll("\\u00A0", "");

                   if (!je.contains("-")) {
                       var je1 = Double.parseDouble(je);
                       moneySum = moneySum + je1;
                       context.put("moneySum",moneySum);
                       billAllData.setNewCharges(new BigDecimal(String.format("%.2f", moneySum)));
                   }else if(je.contains("-")){
                       //本期已还款金额===对应的上期已还款金额(last_payment)
                       pay = pay + Double.parseDouble(je.replaceAll("-", ""));
                       context.put("pay", pay);
                   }
                   expense.setId(UUID.randomUUID().toString());
                   expense.setCardNum(creditCardAllData.getCardNum());// 卡号后四位
                   expense.setDescription(zy);
                   // 交易时间
                   expense.setTransDate(
                       yyyy_MM_dd_HH_mm_ss_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-")
                           + " 00:00:00"));
                   // 以下没数据
                   expense.setPostDate(
                       yyyy_MM_dd_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-")));

                   expense.setAmountMoney(new BigDecimal(je));// 交易金额
                   expense.setOrderIndex(++index);// 索引
                   expense.setBalance(null);
                   expense.setTransAddr("");
                   expense.setTransMethod("");
                   expense.setTransChannel("");
                   expense.setCategory(classify(zy,expense.getAmountMoney()));
                   expense.setRemark("");
                   expense.setOppositeBank("");
                   expense.setNameOnOppositeCard("");
                   expense.setOppositeCardNo("");
                   expense.setOppositeBank("");
                   expense.setCurrencyType("CNY");// 交易币种
                   shopping_sheets.add(expense);
                   billAllData.setShoppingSheets(shopping_sheets);

                   //将第一页的index的最后一条的数量 放到上下文中
                   context.put("billIndexCount",index);

                   if (isNumExists(zy) && (zy.contains("账单分期") || (!zy.contains(":") && zy.contains("客服") && !zy.contains("：")  && !zy.contains("转分期")) || zy.contains("现分") || (zy.contains("电销") && !zy.contains("转分期")) || zy.contains("短信消")
                       || zy.contains("中国铁路") || zy.contains("新兴业") || zy.contains("智付电子") || zy.contains("手机") || zy.contains("巴龙庄园") || zy.contains("波尔多") || zy.contains("网易宝") || zy.contains("波拿大帝")  || zy.contains("微信")
                   )) {//分期
                       if(getFen(zy).size() > 1){
                           ins = new Installment();
                           billAllData.getInstallments().add(ins);
                           ins.setShoppingsheetId(UUID.randomUUID().toString());
                           // 本期金额
                           ins.setAmountMoney(new BigDecimal(je));
                           // 记账日期
                           ins.setPostDate(yyyy_MM_dd_PARSER.parse(jyr.replaceAll("/", "-")));
                           // 交易日期
                           ins.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(jyr.replaceAll("/", "-") + " 00:00:00"));
                           // 分期描述
                           ins.setInstallmentDesc(zy);
                           // 分期转账手续费描述
                           ins.setTransferfeeDesc("");
                           // 分期类型
                           ins.setInstallmentType("BILL");
                           // 币种
                           ins.setCurrencyType("CNY");
                           // 转账手续费
                           ins.setTransferFee(null);

                           if((i + 1) <= mxtrs.size() - 6){
                               var element = mxtrs.get(i+1).children();
                               if(element.size() == 5 ){

                                   var handFeeDes = element.get(3).text().trim().replaceAll("\\u00A0", "");
                                   logger.info("{},这是下一条,手续费描述:{}",context.get(Constants.LUA_TASK_KEY),handFeeDes);
                                   var handFee = element.get(2).text().trim();//下一条的手续费
                                   if (!handFeeDes.matches(".*第\\d*期共\\d*期") &&  !handFeeDes.contains("分期付款费用") ){
                                       continue;
                                   }
                                   if(getFen(handFeeDes).size() > 1 && getFen(handFeeDes).get(0) == getFen(zy).get(1)){

                                       if(handFeeDes.startsWith("分期付款费用")){
                                           ins.setHandingFee(new BigDecimal(handFee));// 手续费
                                           ins.setHandingfeeDesc(handFeeDes);// 分期手续费描述
                                       }
                                   }
                                   if((i - 1) <= mxtrs.size() - 6){
                                       var element1 = mxtrs.get(i-1).children();
                                       var handFeeDesUp = element1.get(3).text().trim().replaceAll("\\u00A0", "");
                                       logger.info("{},这是上一条,手续费描述:{}",context.get(Constants.LUA_TASK_KEY),handFeeDesUp);
                                       var handFeeUp = element1.get(2).text().trim();
                                       if (!handFeeDesUp.matches(".*第\\d*期共\\d*期") &&  !handFeeDesUp.contains("分期付款费用")){
                                           continue;
                                       }
                                       //TO:防止上一条的分期付款费用不是本期账单的手续费
                                       if(getFen(handFeeDesUp).size() > 1 && getFen(handFeeDesUp).get(0) == getFen(zy).get(1)){

                                           if(handFeeDesUp.startsWith("分期付款费用")){
                                               ins.setHandingFee(new BigDecimal(handFeeUp));// 手续费
                                               ins.setHandingfeeDesc(handFeeDesUp);// 分期手续费描述
                                           }
                                       }
                                   }
                               }
                               if((i - 1) <= mxtrs.size() - 6){
                                   var element1 = mxtrs.get(i-1).children();
                                   var handFeeDes = element1.get(3).text().trim().replaceAll("\\u00A0", "");
                                   logger.info("{},这是上一条,手续费描述:{}",context.get(Constants.LUA_TASK_KEY),handFeeDes);
                                   var handFee = element1.get(2).text().trim();
                                   if(handFeeDes.startsWith("分期付款费用")){
                                       ins.setHandingFee(new BigDecimal(handFee));// 手续费
                                       ins.setHandingfeeDesc(handFeeDes);// 分期手续费描述
                                   }
                               }
                           }
                           // if(isNumExists(zy)){
                           if (getFen(zy).size() == 3) {
                               //当前分期数
                               ins.setCurrentMonth(getFen(zy).get(1));
                               // 总分期数
                               ins.setTotalMonth(getFen(zy).get(2));
                           } else if (getFen(zy).size() == 2){
                               ins.setCurrentMonth(getFen(zy).get(0));
                               // 总分期数
                               ins.setTotalMonth(getFen(zy).get(1));
                           }

                       }
                   }else if (zy.contains("分期付款费用")) {// 就是分期手续费
                       if(i == 12){
                           context.put("handfee", je);
                           context.put("handfeedes", zy);
                        }else {
                           continue;
					    }
                   }
               }
           }
       }

   }catch(e){
       logger.info("{},解析【{}】月的账单的第一页的消费信息的页面异常：{}",context.get(Constants.LUA_TASK_KEY),everyMonth,e)
       statCaughtExceptionByJS(context, e);
   }
}


/**
 *  【获取每个月账单的第二页以上的消费信息的页面】
 */
function getEveryMonthBillMorePageHtml(everyMonth,j,cardNum){
    logger.info("{},获取【{}】月的账单的第【{}】页的消费信息的页面:",context.get(Constants.LUA_TASK_KEY),everyMonth,j);

    var pageURL = "https://3g.cib.com.cn/app/50003.html";
    var i = 0;
    var param = new HashMap();
        param.put("beginNo", (j - 1) + "1");
        param.put("flowsn", "70");

     do{
         var billPagesHtml =RequestUtil.post(pageURL,param,getHttpPostCreditTwo());
         i ++;
     }while(!billPagesHtml.contains("人民币账单明细") && i < 5);

    storeDataToFile(context,billPagesHtml, cardNum + "_bill_shopping_sheets_" + everyMonth+ "-"+j+"Page.html");

    context.put("billPagesHtml",billPagesHtml);

}


/**
 *  【解析每个月账单的第二页以上的消费信息的页面】
 */
function parseEveryMonthBillMorePageHtml(everyMonth,j,cardNum,creditCardAllData,index,billAllData,shopping_sheets){
    logger.info("{},解析【{}】月的账单的第【{}】页的消费信息的页面:",context.get(Constants.LUA_TASK_KEY),everyMonth,j);

    var billPagesHtml = context.get("billPagesHtml");
    var moneySum = context.get("moneySum");
    logger.info("{},【{}】月的第一页的new_charges的金额:{}",context.get(Constants.LUA_TASK_KEY),everyMonth,moneySum);
    var pay = context.get("pay");
    logger.info("{},【{}】月的第一页的last_payment的金额:{}",context.get(Constants.LUA_TASK_KEY),everyMonth,pay);
    try{
        if( StringUtils.isNotBlank(billPagesHtml) && billPagesHtml.contains("人民币账单明细")){
            var ins1= null ;
            var pageDoc = Jsoup.parse(billPagesHtml);
            var tablesPage = pageDoc.select("table");

            if (tablesPage.size() > 2) {
                var table2 = tablesPage.get(3);
                var mxtrs = table2.select("tr");
                for (var i = 3; i < mxtrs.size() - 6; i++) {
                    var expense = new Expense();
                    var tds = mxtrs.get(i).children();
                    var jyr = tds.get(1).text().trim();
                    var  je = tds.get(2).text().trim();
                    var zy = tds.get(3).text().trim().replaceAll("\\u00A0", "");

                    if (!je.contains("-")) {
                        var je1 = Double.parseDouble(je);

                             moneySum = moneySum + je1;
                        billAllData.setNewCharges(new BigDecimal(String.format("%.2f", moneySum)));
                    }else if(je.contains("-")){
                        ////本期已还款金额===对应的上期已还款金额(last_payment)

                        pay = pay + Double.parseDouble(je.replaceAll("-", ""));
                        context.put("pay", pay);
                    }
                    expense.setCategory(classify(zy,expense.getAmountMoney()));
                    expense.setId(UUID.randomUUID().toString());
                    expense.setCardNum(creditCardAllData.getCardNum());// 卡号后四位
                    expense.setDescription(zy);
                    // 交易时间
                    expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-")
                        .replaceAll("(?<=^\\d{4}\\-\\d{2})", "-") + " 00:00:00"));
                    // 以下没数据
                    expense.setPostDate(yyyy_MM_dd_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-")
                        .replaceAll("(?<=^\\d{4}\\-\\d{2})", "-")));
                    expense.setAmountMoney(new BigDecimal(je));// 交易金额
                    expense.setOrderIndex(++index);// 索引
                    expense.setBalance(null);
                    expense.setTransAddr("");
                    expense.setTransMethod("");
                    expense.setTransChannel("");
                    expense.setRemark("");
                    expense.setOppositeBank("");
                    expense.setNameOnOppositeCard("");
                    expense.setOppositeCardNo("");
                    expense.setOppositeBank("");
                    expense.setCurrencyType("CNY");// 交易币种
                    shopping_sheets.add(expense);
                    billAllData.setShoppingSheets(shopping_sheets);

                    if (isNumExists(zy) && (zy.contains("账单分期") || ( !zy.contains(":") && zy.contains("客服") && !zy.contains("：")  && !zy.contains("转分期")) || zy.contains("现分") || (zy.contains("电销") && !zy.contains("转分期") ) || zy.contains("短信消")
                        || zy.contains("中国铁路") || zy.contains("新兴业") || zy.contains("智付电子") || zy.contains("手机") || zy.contains("巴龙庄园") || zy.contains("波尔多") || zy.contains("网易宝") || zy.contains("波拿大帝")  || zy.contains("微信")
                    )) {// 分期
                        if(getFen(zy).size() > 1){
                            ins1 = new Installment();
                            billAllData.getInstallments().add(ins1);
                            ins1.setShoppingsheetId(UUID.randomUUID().toString());
                            // if (zy.startsWith("分期付款费用")) {

                            // }else {
                            // 记账日期
                            ins1.setPostDate(yyyy_MM_dd_PARSER.parse(jyr.replaceAll("/", "-")));
                            // 交易日期
                            ins1.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(jyr.replaceAll("/", "-") + " 00:00:00"));
                            // 本期金额
                            ins1.setAmountMoney(new BigDecimal(je));
                            // 分期描述
                            ins1.setInstallmentDesc(zy);                        //分期付款费用 8 12
                            // 分期转账手续费描述
                            ins1.setTransferfeeDesc("");
                            // 分期类型
                            ins1.setInstallmentType("BILL");
                            // 币种
                            ins1.setCurrencyType("CNY");
                            // 转账手续费
                            ins1.setTransferFee(null);


                            //客服消费 8 12
                            if((i + 1) <= mxtrs.size() - 6){                     //分期付款费用  9 12
                                var element = mxtrs.get(i+1).children();
                                //长度等于5 是为了防止有上一页这几个字
                                if(element.size() == 5){
                                    var handFeeDes = element.get(3).text().trim().replaceAll("\\u00A0", "");
                                    logger.info("{},这是下一条,手续费描述:{}",context.get(Constants.LUA_TASK_KEY),handFeeDes);
                                    var handFee = element.get(2).text().trim();//分期手续费

                                    if (!handFeeDes.matches(".*第\\d*期共\\d*期") &&  !handFeeDes.contains("分期付款费用")){
                                        continue;
                                    }
                                    //取手续费里面的数字放到集合的长度大于1（防止光有分期付款费用 没有数字）并且分期副快费用中的当前分期等于描述中的当前分期
                                    if(getFen(handFeeDes).size() > 1 && getFen(handFeeDes).get(0) == getFen(zy).get(1) ){

                                        if(handFeeDes.startsWith("分期付款费用")){
                                            ins1.setHandingFee(new BigDecimal(handFee));// 手续费
                                            ins1.setHandingfeeDesc(handFeeDes);// 分期手续费描述
                                        }/*else if(item == 1){
                                             if(!"".equals(hand.get("handfee")) && null != hand.get("handfee")){

                                             ins1.setHandingFee(new BigDecimal(hand.get("handfee")));// 手续费
                                             }
                                             ins1.setHandingfeeDesc(hand.get("handfeedes"));// 分期手续费描述
                                             }*/
                                    }
                                    if ((i - 1) <= mxtrs.size() - 6) {
                                        var element1 = mxtrs.get(i-1).children();
                                        var handFeeDesUp = element1.get(3).text().trim().replaceAll("\\u00A0", "");
                                        logger.info("{},这是上一条,手续费描述:{}",context.get(Constants.LUA_TASK_KEY),handFeeDesUp);
                                        var handFeeUp = element1.get(2).text().trim();
                                        if (!handFeeDesUp.matches(".*第\\d*期共\\d*期") &&  !handFeeDesUp.contains("分期付款费用")){
                                            continue;
                                        }
                                        //TO:防止上一条的分期付款费用不是本期账单的手续费
                                        if(getFen(handFeeDesUp).size() > 1 && getFen(handFeeDesUp).get(0) == getFen(zy).get(1)){

                                            if(handFeeDesUp.startsWith("分期付款费用")){
                                                ins1.setHandingFee(new BigDecimal(handFeeUp));// 手续费
                                                ins1.setHandingfeeDesc(handFeeDesUp);// 分期手续费描述
                                            }
                                        }
                                    }
                                }
                                if((i - 1) <= mxtrs.size() - 6){
                                    var element1 = mxtrs.get(i-1).children();
                                    var handFeeDes = element1.get(3).text().trim().replaceAll("\\u00A0", "");
                                    logger.info("{},这是上一条,手续费描述:{}",context.get(Constants.LUA_TASK_KEY),handFeeDes);
                                    var handFee = element1.get(2).text().trim();
                                    if(handFeeDes.startsWith("分期付款费用")){
                                        ins1.setHandingFee(new BigDecimal(handFee));// 手续费
                                        ins1.setHandingfeeDesc(handFeeDes);// 分期手续费描述
                                    }
                                }
                            }

                            if(i == 3){
                                var je =  context.get("handfee");
                                var des =   context.get("handfeedes");
                                 if(getFen(des).size() > 1 && getFen(des).get(0) == getFen(zy).get(1)){
                                     ins1.setHandingFee(new BigDecimal(je));// 手续费
                                     ins1.setHandingfeeDesc(des);// 分期手续费描述
                                 }
                            }

                            // if(isNumExists(zy)){
                            if (getFen(zy).size() == 3) {
                                // 总分期数
                                ins1.setTotalMonth(getFen(zy).get(2));
                                ins1.setCurrentMonth(getFen(zy).get(1));
                            } else if (getFen(zy).size() == 2){
                                ins1.setCurrentMonth(getFen(zy).get(0));
                                // 总分期数
                                ins1.setTotalMonth(getFen(zy).get(1));
                            }


                            // }
                        }
                    } else if (zy.contains("分期付款费用")) {
                        continue;
                    }
                }
            }
        }
    }catch (e) {
        logger.info("{},解析【{}】月的账单的第【{}】页的消费信息的页面异常：{}",context.get(Constants.LUA_TASK_KEY),everyMonth,j,e);
        statCaughtExceptionByJS(context, e);
    }
}


/**
 * 【处理已出账单的最终结果】
 */
function handleEveryMonthBillResult(billAllData,billEveryMonth,creditCardAllData,i) {
    logger.info("{},处理【{}】月已出账单的最终结果",context.get(Constants.LUA_TASK_KEY),billEveryMonth);

    // 账单月份 yyyy-MM
    billAllData.setBillMonth(billEveryMonth);
    // 账单日yyyy-MM-dd
    billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(billEveryMonth + "-" + date));
    billAllData.setCreditLimit(creditCardAllData.getCreditLimit());
    billAllData.setCashLimit(creditCardAllData.getCashLimit());
    billAllData.setUsdNewBalance(null);
    billAllData.setUsdMinPayment(null);
    billAllData.setUsdLastBalance(null);
    billAllData.setUsdLastPayment(null);
    billAllData.setUsdNewCharges(null);
    billAllData.setUsdInterest(null);
    billAllData.setInterest(null);
    billAllData.setUsdCreditLimit(null);
    billAllData.setUsdCashLimit(null);

    var lastPayment = context.get("pay");
    if (lastPayment !==  null) {
        last_payment = new BigDecimal("-" + String.format("%.2f", lastPayment));
    } else {
        last_payment = new BigDecimal(0.0);
    }
    billAllData.setLastPayment(last_payment);
    if(i == 0){
        billAllData.setLastBalance(null);
    }else {
        billAllData.setLastBalance(last_balance);
    }
    last_balance = billAllData.getNewBalance();

}


/**
 * 【获取未出账单的第一页的页面】
 */
function getUndoneFirstHtml(cardNum){
    logger.info("{},获取未出账单第一页的页面",context.get(Constants.LUA_TASK_KEY));

    var fiveUrl = "https://3g.cib.com.cn/app/10000.html";

    var param = new HashMap();
        param.put("flowsn", "50");
        param.put("secmenu", "0300001052");

    var unDoneFirstHtml = RequestUtil.post(fiveUrl,param,getHttpPostCreditOne());

    storeDataToFile(context,unDoneFirstHtml, cardNum + "_bill_out_1Page.html");

    context.put("unDoneFirstHtml",unDoneFirstHtml);

}


/**
 * 【解析未出账单的第一页的页面】
 */
function parseUndoneFirstHtml(shopping_sheets,creditCardAllData,billAllDataWithOut,lastPayment,newCharges,newBalance) {
    logger.info("{},解析未出账单第一页的页面",context.get(Constants.LUA_TASK_KEY));

  var unDoneFirstHtml  = context.get("unDoneFirstHtml");

  try{
      if(StringUtils.isNotBlank(unDoneFirstHtml) && unDoneFirstHtml.contains("人民币未出账单")){

          var docs = Jsoup.parse(unDoneFirstHtml);
          //获取当前页和总页数 ===[1/2]
          var undoneTotalPages = docs.select("form:nth-child(2)").text().replaceAll("下一页", "").trim();
          context.put("undoneTotalPages",undoneTotalPages);

          var tablem = docs.select("table");
          if (tablem.size() == 2){
              return;
          }

          var tables = docs.select("table");
          var index = 1;

          if (tables.size() > 1) {
              var table2 = tables.get(2);
              var mxtrs = table2.select("tr");


              for (var i = 3; i < mxtrs.size() - 2; i++) {
                  var tds = mxtrs.get(i).children();
                  var expense = new Expense();
                  var jyr = tds.get(1).text().trim();
                  var je = tds.get(2).text().trim().replaceAll("\\s*", "");
                  var zy = tds.get(3).text().trim();
                  newBalance = Double.parseDouble(je) + newBalance;
                  context.put("newBalanceUndone",newBalance);

                  if(zy.contains("转入") || zy.contains("还款")){
                      lastPayment = Double.parseDouble(je) + lastPayment ;
                      context.put("lastPaymentUndone",lastPayment);
                  }

                  if (!je.contains("-")) {
                      newCharges = Double.parseDouble(je) + newCharges ;
                      context.put("newChargesUndone",newCharges);
                  }
                  expense.setId(UUID.randomUUID().toString());
                  expense.setCardNum(creditCardAllData.getCardNum());// 卡号后四位
                  expense.setCurrencyType("CNY");// 交易币种
                  expense.setAmountMoney(new BigDecimal(je));// 交易金额
                  expense.setOrderIndex(index ++);// 索引
                  // 交易时间
                  expense.setTransDate(
                      yyyy_MM_dd_HH_mm_ss_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-")
                          + " 00:00:00"));
                  // 以下没数据
                  expense.setPostDate(
                      yyyy_MM_dd_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-")));
                  expense.setCategory(classify(zy,expense.getAmountMoney()));
                  expense.setBalance(null);
                  expense.setTransAddr("");
                  expense.setTransMethod("");
                  expense.setTransChannel("");
                  expense.setRemark("");
                  expense.setDescription(zy);
                  expense.setOppositeBank("");
                  expense.setNameOnOppositeCard("");
                  expense.setOppositeCardNo("");
                  shopping_sheets.add(expense);
                  billAllDataWithOut.setShoppingSheets(shopping_sheets);

              }
              context.put("index",index);
          }
      }
  }catch (e) {
      logger.info("{},解析未出账单第一页的页面异常：{}",context.get(Constants.LUA_TASK_KEY),e);
      statCaughtExceptionByJS(context, e);
  }


}


/**
 * 【获取未出账单第一页以上的页面】
 */
function  getUndoneMorePagesHtml(cardNum,currentPage){
    logger.info("{},获取未出账单第【{}】页的页面",context.get(Constants.LUA_TASK_KEY),currentPage);

    var pageUrl = "https://3g.cib.com.cn/app/50020.html";

    var param = new HashMap();
    param.put("beginNo", "11");
    param.put("flowsn", "70");
    var undoneMorePagesHtml = RequestUtil.post(pageUrl,param,getHttpPostCreditThree());

    storeDataToFile(context,undoneMorePagesHtml, cardNum + "_bill_out_"+ currentPage +"Page.html");

    context.put("undoneMorePagesHtml",undoneMorePagesHtml);
}


/**
 * 【解析未出账单第一页以上的页面】
 */
function  parseUndoneMorePagesHtml(currentPage,billAllDataWithOut,shopping_sheets,creditCardAllData,indexUndone){
    logger.info("{},解析未出账单第【{}】页的页面",context.get(Constants.LUA_TASK_KEY),currentPage);

   var undoneMorePagesHtml =  context.get("undoneMorePagesHtml");
   var  newBalance = context.get("newBalanceUndone");
   var  lastPayment = context.get("lastPaymentUndone");
   var  newCharges = context.get("newChargesUndone");


   try{
       if(StringUtils.isNotBlank(undoneMorePagesHtml)  && undoneMorePagesHtml.contains("人民币未出账单")){

           var pageHtmlDc = Jsoup.parse(undoneMorePagesHtml);

           var tablesPage = pageHtmlDc.select("table");
           if (tablesPage.size() > 1) {
               var table2 = tablesPage.get(2);
               var mxtrs = table2.select("tr");
               for (var i = 3; i < mxtrs.size() - 2; i++) {
                   var tds = mxtrs.get(i).children();
                   var expense = new Expense();
                   var jyr = tds.get(1).text().trim();
                   var je = tds.get(2).text().trim().replaceAll("\\s*", "");
                   var zy = tds.get(3).text().trim();
                   newBalance = Double.parseDouble(je) + newBalance;
                   context.put("newBalanceUndone",newBalance);

                   if(zy.contains("转入") || zy.contains("还款")){
                       lastPayment = lastPayment + Double.parseDouble(je);
                       context.put("lastPaymentUndone",lastPayment);
                   }

                   if (!je.contains("-")) {
                       newCharges = Double.parseDouble(je) + newCharges ;
                       context.put("newChargesUndone",newCharges);
                   }
                   expense.setAmountMoney(new BigDecimal(je));// 交易金额
                   expense.setId(UUID.randomUUID().toString());
                   expense.setCardNum(creditCardAllData.getCardNum());// 卡号后四位
                   expense.setCurrencyType("CNY");// 交易币种
                   expense.setOrderIndex(++indexUndone);// 索引
                   // 交易时间
                   expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-")
                       .replaceAll("(?<=^\\d{4}\\-\\d{2})", "-") + " 00:00:00"));
                   // 以下没数据
                   expense.setPostDate(yyyy_MM_dd_PARSER.parse(processDate(jyr).replaceAll("(?<=^\\d{4})", "-")
                       .replaceAll("(?<=^\\d{4}\\-\\d{2})", "-")));
                   expense.setCategory(classify(zy,expense.getAmountMoney()));
                   expense.setBalance(null);
                   expense.setTransAddr("");
                   expense.setTransMethod("");
                   expense.setTransChannel("");
                   expense.setRemark("");
                   expense.setDescription(zy);
                   expense.setOppositeBank("");
                   expense.setNameOnOppositeCard("");
                   expense.setOppositeCardNo("");
                   shopping_sheets.add(expense);
                   billAllDataWithOut.setShoppingSheets(shopping_sheets);
               }
           }
       }
   }catch (e) {
       logger.info("{},解析未出账单第【{}】页的页面异常：{}",context.get(Constants.LUA_TASK_KEY),currentPage,e);
       statCaughtExceptionByJS(context, e);
   }



}


/**
 * 【处理未出账单】
 */
function  handleUndoneBill(billAllDataWithOut,creditCardAllData){
    logger.info("{},处理最终的未出账单",context.get(Constants.LUA_TASK_KEY));

    var  newBalance = context.get("newBalanceUndone");
         newBalance =  null == newBalance ? 0.0 :newBalance;

     var  lastPayment = context.get("lastPaymentUndone");
          lastPayment =  null == lastPayment ? 0.0 :lastPayment;

    var  newCharges = context.get("newChargesUndone");
         newCharges =  null == newCharges ? 0.0 :newCharges;


    billAllDataWithOut.setCreditLimit(creditCardAllData.getCreditLimit());
    billAllDataWithOut.setCashLimit(creditCardAllData.getCashLimit());
    billAllDataWithOut.setUsdNewBalance(null);
    billAllDataWithOut.setUsdMinPayment(null);
    billAllDataWithOut.setUsdLastBalance(null);
    billAllDataWithOut.setUsdLastPayment(null);
    billAllDataWithOut.setUsdNewCharges(null);
    billAllDataWithOut.setUsdInterest(null);
    billAllDataWithOut.setUsdCreditLimit(null);
    billAllDataWithOut.setUsdCashLimit(null);
    billAllDataWithOut.setMinPayment(null);
    billAllDataWithOut.setInterest(null);
    billAllDataWithOut.setLastPayment(null);



    var calendar = Calendar.getInstance();
    var day = calendar.get(Calendar.DAY_OF_MONTH);
    day =  Integer.parseInt(day);
    if( date !== ""){
        if (day > Integer.parseInt(date)) {
            calendar.add(Calendar.MONTH, 1);
        }
    }
    var billdate = creditSdf.format(calendar.getTime());

    billAllDataWithOut.setBillMonth(billdate);
    billAllDataWithOut.setBillDate(yyyy_MM_dd_PARSER.parse(billdate + "-" + date));
    billAllDataWithOut.setPaymentDueDate(null);


    var sumBalance =  context.get("sumBalance");
    logger.info("{},上期-----:{}",context.get(Constants.LUA_TASK_KEY),sumBalance);
    if(sumBalance !== null){
        newBalance = newBalance +  Double.parseDouble(sumBalance);
    }
    billAllDataWithOut.setLastPayment(0.0 == lastPayment ? null : new BigDecimal( String.format("%.2f", lastPayment)));
    billAllDataWithOut.setNewBalance(0.0 == newBalance ? null :new BigDecimal(String.format("%.2f", newBalance)));
    billAllDataWithOut.setNewCharges(0.0 == newCharges ? null : new  BigDecimal(String.format("%.2f", newCharges)));
    billAllDataWithOut.setLastBalance(0.0 !== context.get("sumBalance") ? new BigDecimal(context.get("sumBalance")):null);

}



/**
 * 借记卡获取6个月的第一天  格式:2017-01-01
 * @return
 * @throws Exception
 */
function  getMonthFirstDay(){
    var lst = new ArrayList();

    var calendar = new GregorianCalendar();

    for (var i = 0; i < 12; i++) {
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1 * i);

        var gcLast =  Calendar.getInstance();
        gcLast.setTime(calendar.getTime());
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        lst.add(depoitSdf.format(gcLast.getTime()));
    }
    return lst;
}

/**
 * 借记卡获取六个月的最后一天,当月就返回到今天
 */
function  getMonthLastDay(){
    var lst = new ArrayList();

    var calendar = new GregorianCalendar();
//		DateFormat dfdate = new SimpleDateFormat("yyyy-MM-dd");
    for (var i = -1; i < 11; i++) {

        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1 * i);

        var gcLast =  Calendar.getInstance();
        gcLast.setTime(calendar.getTime());
        gcLast.set(Calendar.DATE, 1);
        gcLast.add(Calendar.DATE, -1);
        if (gcLast.getTime().after(new Date())) {
            lst.add(depoitSdf.format(new Date()));
        } else {
            lst.add(depoitSdf.format(gcLast.getTime()));
        }
    }
    return lst;
}



/**
 * 【获取账单日页面】
 *
 * @throws IOException
 * @throws ClientProtocolException
 */
function  getDateHtml()  {
    logger.info("{},获取账单日页面",context.get(Constants.LUA_TASK_KEY));
    var i = 0;
    var map = new HashMap();

    var dataURL = "https://3g.cib.com.cn/app/10000.html";
        map.put("flowsn", "50");
        map.put("secmenu", "0300001054");

    var hear = new HashMap();
    hear.put("Referer", "https://3g.cib.com.cn/app/10000.html");
    hear.put("Origin", "https://3g.cib.com.cn");
    hear.put("Host", "3g.cib.com.cn");
    hear.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");

    do{
        var dateHtml = RequestUtil.post(dataURL,map,hear);
        i ++;
    }while(!dateHtml.contains("账单日查询") && i < 3);

    storeDataToFile(context,dateHtml, "billdata.html");

    context.put("getDateHtml",dateHtml);


}



/*
 * 解析【账单日的页面】   获取账单日
 */
function parseDateHtml(){
    logger.info("{},开始解析账单日页面,并获取账单日",context.get(Constants.LUA_TASK_KEY));

    try{
        var dateHtml =  context.get("getDateHtml");
        if(StringUtils.isNotBlank(dateHtml) && dateHtml.contains("账单日查询")) {

            var dateDoc = Jsoup.parse(dateHtml);
            var table = dateDoc.select("table.MBcontent1:nth-child(1)").first();
            var dataText = "";
            if (table !== null) {
                var trs = table.select("tr");
                if (trs.size() == 6) {// li
                    dataText = dateDoc
                        .select("body > div:nth-child(3) > form > table > tbody > tr:nth-child(4) > td:nth-child(3)").text()
                        .trim();
                } else if (trs.size() == 7) {// han
                    dataText = dateDoc
                        .select("table.MBcontent1:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(3)").text()
                        .trim();
                }
            }
        }
        return getGroupByIndex("[0-9]{2}", dataText, 0);
    }catch (e) {
        logger.info("{},解析账单日页面异常:{}",context.get(Constants.LUA_TASK_KEY),e);
        statCaughtExceptionByJS(context, e);
    }
}


/////////////////////////////////////////////////////////【借记卡】///////////////////////////////////////

/**
 * 【获取卡号、开户行、持卡人页面】
 */
function  getBaseInfoHtml(){
    logger.info("{},开始获取借记卡【卡号、开户行、持卡人】页面",context.get(Constants.LUA_TASK_KEY));

    var openBankURL = "https://3g.cib.com.cn/app/20901.html" ;

    var param = new HashMap();
    param.put("flowsn", "0");
    param.put("listcardno", "0");
    param.put("listno", "0");

    var openBankHtml =RequestUtil.post(openBankURL,param,getHttpPostTwo());
    storeDataToFile(context,openBankHtml,   "openBankXml.html");

    context.put("openBankHtml",openBankHtml);
    if(openBankHtml.contains("该业务仅支持借记卡办理，请使用借记卡登录后继续办理。")){
        throw  new BankException(BALO_99604_15, errorMsgMap.get(BALO_99604_15));
    }

}

/**
 * 【解析卡号、开户行、持卡人页面】
 */
function  parseBaseInfoHtml(creditCardAllData){
    logger.info("{},开始解析借记卡【卡号、开户行、持卡人】页面",context.get(Constants.LUA_TASK_KEY));

    var  openBankHtml = context.get("openBankHtml");
    try{
        if(StringUtils.isNotBlank(openBankHtml)){
            var openBankDoc = Jsoup.parse(openBankHtml);

            var openBank = openBankDoc.select("body > div.content > div.purchase-info > ul > li.top-inline.clearfix > span").text().trim().replaceAll("\\u00A0", "");
            var fullcardNum = openBankDoc.select("body > div.content > form > div.card-info.card_bg > div.card-account > em:nth-child(2)").text().trim().replaceAll("\\s*", "").replaceAll("\\u00A0", "");
            var name = openBankDoc.select("body > div.content > form > div.card-info.card_bg > div:nth-child(2) > span.fl.mat5").text().trim().replaceAll("\\u00A0", "");;

            //完整卡号
            creditCardAllData.setFullCardNum(fullcardNum);
            //卡号后四位
            creditCardAllData.setCardNum(fullcardNum.substring((fullcardNum.length()-4), (fullcardNum.length())));
            //开户行
            creditCardAllData.setOpenBank(openBank);
            //持卡人
            creditCardAllData.setNameOnCard(name);
        }
    }catch (e) {
        logger.info("{},解析借记卡基本信息页面异常：{}",context.get(Constants.LUA_TASK_KEY),e);
        statCaughtExceptionByJS(context, e);
    }
}


/**
 * 【获取余额页面】
 */
function getBalanceHtml(){
    logger.info("{},开始获取兴业借记卡【余额】页面",context.get(Constants.LUA_TASK_KEY));

    //var huoTwoUrl = "https://3g.cib.com.cn/app/10020.html";
    var huoTwoUrl =  "https://3g.cib.com.cn/app/80820.html";
    var param = new HashMap();
    /* param.put("flowsn", "201");
     param.put("listcardno", "0");
     param.put("listno", "0");
     param.put("secmenu", "0200000302");
     param.put("menu", "0100000020");
     param.put("isJump", "1");*/
    param.put("menu","0100000020");

    // var yueHtml = RequestUtil.post(huoTwoUrl,param,getHttpPostOne());
    var yueHtml = RequestUtil.post(huoTwoUrl,param,getHttpPostTwo());
    storeDataToFile(context,yueHtml,  "card_yue.html");

    context.put("yueHtml",yueHtml);
}


/**
 * 【解析余额页面】
 */
function parseBalanceHtml(creditCardAllData){
    logger.info("{},开始解析兴业借记卡【余额】页面",context.get(Constants.LUA_TASK_KEY));

    var yueHtml =  context.get("yueHtml");
    try {
        if(StringUtils.isNotBlank(yueHtml)){

            var yueDoc = Jsoup.parse(yueHtml);
            var balance = yueDoc.select("body > div.content > form > div > div > div:nth-child(3) > strong > em").text().trim().replaceAll(",","");


            creditCardAllData.setBalance(new BigDecimal(balance));
        }

    }catch (e) {
        logger.info("{},解析兴业借记卡【余额】页面异常：{}",context.get(Constants.LUA_TASK_KEY),e);
        statCaughtExceptionByJS(context, e);
    }

}


/**
 * 【获取开卡时间页面】
 */
function getOpenTimeHtml(){
    logger.info("{},开始获取兴业借记卡【开卡时间】页面",context.get(Constants.LUA_TASK_KEY));

    var openTimeBeforeURL = "https://3g.cib.com.cn/app/10020.html";
    var param = new HashMap();
    param.put("flowsn", "201");
    param.put("listcardno", "0");
    param.put("listno", "0");
    param.put("secmenu", "0200000302");
    param.put("menu", "0100000020");
    param.put("isJump", "1");
    RequestUtil.post(openTimeBeforeURL,param,getHttpPostOne());

    var huoqiURL = "https://3g.cib.com.cn/app/10020.html";
    param.clear();
    param.put("flowsn", "250");
    param.put("mxlistno", "001");
    param.put("cardindex", "0");
    var openTimeHtml =RequestUtil.post(huoqiURL,param,getHttpPostThree());
    storeDataToFile(context,openTimeHtml,   "card_openTime.html");

    context.put("openTimeHtml",openTimeHtml);

}


/**
 * 【解析 开卡时间页面】
 */
function parseOpenTimeHtml(creditCardAllData){
    logger.info("{},开始解析兴业借记卡【开卡时间】页面",context.get(Constants.LUA_TASK_KEY));

    var openTimeHtml = context.get("openTimeHtml");
    try {
        if(StringUtils.isNotBlank(openTimeHtml)){
            var openTimeDoc = Jsoup.parse(openTimeHtml);
            //开户时间
            open_time = openTimeDoc.select("body > div.content > div.product-introduce > div:nth-child(4) > div:nth-child(1) > span").text().trim().replaceAll("/", "-");
            // 开户时间
            creditCardAllData.setOpenTime(yyyy_MM_dd_PARSER.parse(open_time));
        }

    }catch (e) {
        logger.info("{},开始解析兴业借记卡【开卡时间】页面异常：{}",context.get(Constants.LUA_TASK_KEY),e);
        statCaughtExceptionByJS(context, e);
    }
}


/**
 * 【获取每个月账单的总信息 包括总页数、第一页的消费的总条数】
 */
function getBillEveryMonthFirstHtml(cardNum,firstDay,lastDay){
    logger.info("{},获取借记卡【{}】月账单的总信息",context.get(Constants.LUA_TASK_KEY),firstDay);


    var baseURL="https://3g.cib.com.cn/app/80820.html";
    var param = new HashMap();
    param.put("flowsn", "50");
    param.put("secmenu", "0200000300");
    RequestUtil.post(baseURL,param,getHttpPostOne());

    var billsDetailTwoURL = "https://3g.cib.com.cn/app/10040.html";
    param.clear();
    param.put("flowsn", "4");
    param.put("listcardno", "0");
    param.put("listno", "0");
    RequestUtil.post(billsDetailTwoURL,param,getHttpPostFour());

    param.clear();
    param.put("flowsn", "15");
    param.put("xxh", "001");
    param.put("begindate", firstDay);
    param.put("enddate",lastDay);
    param.put("qrytype", "1");
    var billEveryMonthTotalMsgHtml = RequestUtil.post(billsDetailTwoURL,param,getHttpPostFour());

    storeDataToFile(context,billEveryMonthTotalMsgHtml, cardNum + "_bill_" + firstDay + "totalMsg.html");
    /**
     * 判断有没有
     */
    if(billEveryMonthTotalMsgHtml.contains("对不起，找不到符合条件的交易明细。")){
        return;
    }

    context.put("billEveryMonthTotalMsgHtml",billEveryMonthTotalMsgHtml);
}


/**
 * 【解析每个月账单的总信息(账单的第一页) 包括总页数、第一页的消费的总条数 】
 */
function parseBillEveryMonthFirstHtml(firstDay) {
    logger.info("{},解析借记卡【{}】月账单的总信息、包括【总页数、第一页的消费的总条数】",context.get(Constants.LUA_TASK_KEY),firstDay);

    var  billEveryMonthTotalMsgHtml = context.get("billEveryMonthTotalMsgHtml");
    try{
        if(StringUtils.isNotBlank(billEveryMonthTotalMsgHtml)) {

            var billEveryMonthFirstPageDoc = Jsoup.parse(billEveryMonthTotalMsgHtml);

            var sumPage = billEveryMonthFirstPageDoc.select("body > div.content > div.mat10 > form:nth-child(3) > p > span > em").text().replaceAll("\\[", "").replaceAll("\\]", "");

            var billsDetailThreeForm = billEveryMonthFirstPageDoc.select("body > div.content > div.mat10 > form:nth-child(1)");
            var children = billsDetailThreeForm.get(0).children();
            context.put("totalPages",sumPage);
            context.put("firstPageExpenseItem",children);

        }

    }catch (e) {
        logger.info("{},解析借记卡【{}】月账单的总信息、包括【总页数、第一页的消费的总条数】的异常：{}",context.get(Constants.LUA_TASK_KEY),firstDay,e);
        statCaughtExceptionByJS(context, e);
    }
}

/**
 * 【获取每个月账单的第一页的每笔消费信息】
 */
function getEveryMonthEveryExpenseHtml(cardNum,children,firstDay){
    logger.info("{},获取借记卡【{}】月账单的总信息（第一页）的每笔消费信息",context.get(Constants.LUA_TASK_KEY),firstDay);
    var billsDetailTwoURL = "https://3g.cib.com.cn/app/10040.html";

    if(null != children){
        for(var i = 2 ; i < children.size();i++) {

            var param = new HashMap();
            param.put("flowsn", "200");
            param.put("listno", (i - 2) + "");
            var billsDetailFour = RequestUtil.post(billsDetailTwoURL, param, getHttpPostOne());
            storeDataToFile(context, billsDetailFour, cardNum + "_bill_firstPage_shopping_sheets_" +firstDay+"月"+ (i - 1) + "Item.html");

            context.put(i,billsDetailFour);
        }
    }
}

/**
 * 【解析每个月账单的第一页的每笔消费信息】
 */
function parseEveryMonthEveryExpenseHtml(cardNum,children,firstDay,billAllData,creditCardAllData){
    logger.info("{},解析借记卡【{}】月账单的总信息【第一页】的每笔消费信息",context.get(Constants.LUA_TASK_KEY),firstDay);

    var order = 0;

    try{
        if(null != children ) {
            for (var i = 2; i < children.size(); i++) {
                var expense = new Expense();
                var everyMonthFirstPageExpenseItem = context.get(i);


                if (StringUtils.isNotBlank(everyMonthFirstPageExpenseItem)) {

                    var billEveryMonthFirstPageExpenseDoc = Jsoup.parse(everyMonthFirstPageExpenseItem);

                    //记账时间
                    var post_date = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(3) > div:nth-child(2) > span").text().trim().replaceAll("/", "-");

                    //交易时间
                    var trans_date = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(3) > div:nth-child(1) > span").text().trim().replaceAll("/", "-");

                    //交易金额
                    var amount_moneyText = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(1) > div > p").text().trim();
                    var amount_money = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(1) > div > span").text().trim().replaceAll(",", "");


                    //账户余额
                    var balance = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(2) > div:nth-child(2) > span").text().trim().replaceAll(",", "");

                    //对方信息   包括对方持卡人 和对方卡号      (步延伟5218991349688785)
                    var name_on_opposite_card = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(4) > div > span").text().trim();
                    var p = Pattern.compile("([\u4e00-\u9fa5]+)");
                    var m = p.matcher(name_on_opposite_card);
                    var sb = new StringBuffer("");
                    while (m.find()) {
                        sb.append(URLDecoder.decode(m.group(1), "UTF-8"));
                    }

                    //对方卡号
                    var opposite_card_no = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(4) > div > span > span").text().trim();

                    //对方银行
                    var opposite_bank = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(5) > div > span").text().trim();

                    //交易摘要
                    var description = billEveryMonthFirstPageExpenseDoc.select("body > div.content > div.product-introduce > div:nth-child(2) > div:nth-child(1) > span").text().trim();
                    if (amount_moneyText.contains("收入")) {
                        amount_money = "-" + amount_money;
                    } else if (amount_moneyText.contains("支出")) {
                        amount_money = "+" + amount_money;
                    }
                    //备注
                    var remark = billEveryMonthFirstPageExpenseDoc.select(" body > div.content > div.product-introduce > div:nth-child(6) > div > span").text().trim();

                    // 消费id
                    expense.setId(UUID.randomUUID().toString());
                    // 卡号末四位
                    expense.setCardNum(creditCardAllData.getCardNum());
                    // 记账日期
                    expense.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));

                    // 排序
                    expense.setOrderIndex(++order);
                    // 交易时间
                    expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(trans_date));
                    // 币种
                    expense.setCurrencyType("CNY");
                    // 消费金额
                    expense.setAmountMoney(new BigDecimal(amount_money));
                    expense.setCategory(classify(description,expense.getAmountMoney()));
                    // 余额
                    expense.setBalance(new BigDecimal(balance));
                    // 交易地点
                    expense.setTransAddr("");
                    // 交易方式
                    expense.setTransMethod("");
                    // 交易通道
                    expense.setTransChannel("");
                    // 描述
                    expense.setDescription(description);
                    // 备注
                    expense.setRemark(remark);
                    // 对方卡号
                    expense.setOppositeCardNo(opposite_card_no);
                    // 对方银行
                    expense.setOppositeBank(opposite_bank);
                    // 对方户名
                    expense.setNameOnOppositeCard(sb.toString());

                    billAllData.getShoppingSheets().add(expense);

                }
                //将每笔消费的序号添加到上下文中，方便第二页用
                context.put("order", order)
            }
        }
    }catch (e) {
        logger.info("{},解析借记卡【{}】月账单的总信息【第一页】的每笔消费信息异常：{}",context.get(Constants.LUA_TASK_KEY),firstDay,e);
        statCaughtExceptionByJS(context, e);
    }

}


/**
 * 【获取每个月账单 第二页 及更多的页 的 当页 的总条数】
 */
function getEveryMonthBillMorePageSumItemHtml(cardNum,firstDay,currentPage) {
    logger.info("{},获取借记卡【{}】月账单的第【{}】页的消费总条数",context.get(Constants.LUA_TASK_KEY),firstDay,currentPage);
    var billsDetailTwoURL = "https://3g.cib.com.cn/app/10040.html";

    var param = new HashMap();
    param.put("beginNo",  (currentPage - 1) + "1");
    param.put("flowsn", "75");
    var billsDetailsNextPageHtml = RequestUtil.post(billsDetailTwoURL,param,getHttpPostFour());
    storeDataToFile(context,billsDetailsNextPageHtml, cardNum + "_bill_shopping_sheets_"+ firstDay+"月"+currentPage+ "Page.html");

    context.put("billsDetailsNextPageHtml",billsDetailsNextPageHtml);
}


/**
 * 【解析每个月账单 第二页 及更多的页 的消费 总数】
 */
function parseEveryMonthBillMorePageSumItemHtml(cardNum,firstDay,currentPage) {
    logger.info("{},解析借记卡【{}】月账单的第【{}】页的消费总条数",context.get(Constants.LUA_TASK_KEY),firstDay,currentPage);


    try{
        var  billsDetailsNextPageHtml =  context.get("billsDetailsNextPageHtml");
        if(StringUtils.isNotBlank(billsDetailsNextPageHtml)){

            var billsDetailsNextPageDoc = Jsoup.parse(billsDetailsNextPageHtml);
            var billsDetailsNextPage = billsDetailsNextPageDoc.select("body > div.content > div.mat10 > form:nth-child(1)");
            var childs = billsDetailsNextPage.get(0).children();

            context.put("morePageSumItem",childs);
        }
    }catch (e) {
        logger.info("{},解析借记卡【{}】月账单的第【{}】页的消费总条数 异常：{}",context.get(Constants.LUA_TASK_KEY),firstDay,currentPage,e);
        statCaughtExceptionByJS(context, e);
    }
}


/**
 * 【每个月账单的 第二页及更多的页数   每页上的每笔消费】
 */
function getEveryMonthBillMorePageExpenseHtml(cardNum,firstDay,currentPage,childs){
    logger.info("{},获取借记卡【{}】月账单的【{}】页的每笔消费详情的页面",context.get(Constants.LUA_TASK_KEY),firstDay,currentPage);

    if(null != childs) {
        var billsDetailTwoURL = "https://3g.cib.com.cn/app/10040.html";
        for (var i = 2; i < childs.size(); i++) {

            var param = new HashMap();
            param.put("flowsn", "200");
            param.put("listno", (currentPage - 1) + "" + (i - 2));
            var billsDetailFour = RequestUtil.post(billsDetailTwoURL, param, getHttpPostOne());
            storeDataToFile(context, billsDetailFour, cardNum + "_bill_shopping_sheets_" +firstDay+"月"+currentPage+"Page"+((currentPage - 1) + "" + (i - 1)) + ".html");

            context.put(i,billsDetailFour);
        }
    }
}


/**
 * 【解析每个月账单的  第二页及更多的页数  每页上的每笔消费】
 */
function  parseEveryMonthBillMorePageExpenseHtml(firstDay,currentPage,childs,billAllData,creditCardAllData){
    logger.info("{},解析借记卡【{}】月账单的第【{}】页的每笔消费详情的页面",context.get(Constants.LUA_TASK_KEY),firstDay,currentPage);

    var order = 10;
    try{
        if(null != childs) {

            for (var i = 2; i < childs.size(); i++) {
                var expense = new Expense();

                var billsDetailFour = context.get(i);

                if (StringUtils.isNotBlank(billsDetailFour)) {


                    var billsDetailFourDoc = Jsoup.parse(billsDetailFour);
                    //记账时间
                    var post_date = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(3) > div:nth-child(2) > span").text().trim().replaceAll("/", "-");
                    //交易时间
                    var trans_date = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(3) > div:nth-child(1) > span").text().trim().replaceAll("/", "-");

                    //交易摘要
                    var description = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(2) > div:nth-child(1) > span").text().trim();

                    //交易金额
                    var amount_moneyText = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(1) > div > p").text().trim();
                    var amount_money = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(1) > div > span").text().trim().replaceAll(",", "");
                    if (amount_moneyText.contains("收入")) {
                        amount_money = "-" + amount_money;
                    } else if (amount_moneyText.contains("支出")) {
                        amount_money = "+" + amount_money;
                    }

                    //账户余额
                    var balance = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(2) > div:nth-child(2) > span").text().trim().replaceAll(",", "");
                    //对方信息   包括对方持卡人 和对方卡号      (步延伟5218991349688785)
                    var name_on_opposite_card = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(4) > div > span").text().trim();
                    var p = Pattern.compile("([\u4e00-\u9fa5]+)");
                    var m = p.matcher(name_on_opposite_card);
                    var sb = new StringBuffer("");
                    var str = "";
                    while (m.find()) {
                        sb.append(URLDecoder.decode(m.group(1), "UTF-8"));
//		        	str+=m.group(1);
                    }

                    //对方卡号
                    var opposite_card_no = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(4) > div > span > span").text().trim();

                    //对方银行
                    var opposite_bank = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(5) > div > span").text().trim();

                    //备注
                    var remark = billsDetailFourDoc.select("body > div.content > div.product-introduce > div:nth-child(6) > div > span").text().trim();

                    // 消费id
                    expense.setId(UUID.randomUUID().toString());
                    // 卡号末四位
                    expense.setCardNum(creditCardAllData.getCardNum());
                    // 记账日期
                    expense.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));
                    // 排序
                    expense.setOrderIndex(++order);
                    // 交易时间
                    expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(trans_date));
                    // 币种
                    expense.setCurrencyType("CNY");
                    // 消费金额
                    expense.setAmountMoney(new BigDecimal(amount_money));
                    expense.setCategory(classify(description,expense.getAmountMoney()));
                    // 余额
                    expense.setBalance(new BigDecimal(balance));
                    // 交易地点
                    expense.setTransAddr("");
                    // 交易方式
                    expense.setTransMethod("");
                    // 交易通道
                    expense.setTransChannel("");
                    // 描述
                    expense.setDescription(description);
                    // 备注
                    expense.setRemark(remark);
                    // 对方卡号
                    expense.setOppositeCardNo(opposite_card_no);
                    // 对方银行
                    expense.setOppositeBank(opposite_bank);
                    // 对方户名
                    expense.setNameOnOppositeCard(sb.toString());

                    billAllData.getShoppingSheets().add(expense);


                }
            }
        }
    }catch (e) {
        logger.info("{},解析借记卡【{}】月账单的【{}】页的每笔消费详情的页面异常：{}",context.get(Constants.LUA_TASK_KEY),firstDay,currentPage,e);
        statCaughtExceptionByJS(context, e);
    }
}


/**
 * 【处理每个月账单的表头】
 */
function handlerEveryMonth(firstDay,billAllData){
    logger.info("{},处理【{}】月表头",context.get(Constants.LUA_TASK_KEY),firstDay);

    billAllData.setBillMonth(firstDay.substring(0, 7));//格式:yyyy-MM   2017-06
    billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(firstDay));
    billAllData.setNewBalance(null);
    billAllData.setUsdNewBalance(null);
    billAllData.setMinPayment(null);
    billAllData.setUsdMinPayment(null);
    billAllData.setLastBalance(null);
    billAllData.setUsdLastBalance(null);
    billAllData.setLastPayment(null);
    billAllData.setUsdLastPayment(null);
    billAllData.setNewCharges(null);
    billAllData.setUsdNewCharges(null);
    billAllData.setInterest(null);
    billAllData.setUsdInterest(null);
    billAllData.setUsdInterest(null);
    billAllData.setCreditLimit(null);
    billAllData.setUsdCreditLimit(null);
    billAllData.setCashLimit(null);
    billAllData.setUsdCashLimit(null);
}


/**
 * 【获取定期的页面】
 */
function getDepositHtml(){
    logger.info("{}，获取定期活期的页面",context.get(Constants.LUA_TASK_KEY));

    /**
     * 活期
     */
    var huoOne = "https://3g.cib.com.cn/app/80820.html";
    var param = new HashMap();
    param.put("menu", "0100000020");
    param.put("divtype", "");
    RequestUtil.post(huoOne,param,getHttpPostOne());

    var huoTwo = "https://3g.cib.com.cn/app/10020.html";
    param.clear();
    param.put("flowsn", "201");
    param.put("listcardno", "0");
    param.put("listno", "0");
    var huoTwoHtml = RequestUtil.post(huoTwo,param,getHttpPostOne());
    storeDataToFile(context,huoTwoHtml, "card_cunchu.html");

    context.put("deposits",huoTwoHtml);

}

/**
 * 【解析定期的页面】
 */
function parseDepositHtml(creditCardAllData) {
    logger.info("{}，解析定期活期的页面", context.get(Constants.LUA_TASK_KEY));

    try {
        var huoTwoHtml = context.get("deposits");

        var huoTwoDoc = Jsoup.parse(huoTwoHtml);
        var tables = huoTwoDoc.select("table");
        var list = new ArrayList();
        for (var i = 0; i < tables.size() - 1; i++) {
            var deposit = new CreditCardTimeDeposit();
            var text = huoTwoDoc.select("body > div.content > form > table:nth-child(" + (4 + i) + ") > tbody > tr:nth-child(1) > td:nth-child(1)").text().trim();
            if (equals(text, "人民币活期存款")) {
                // 定期金额
                deposit.setBalance(creditCardAllData.getBalance());
                // 定期类型
                deposit.setDepositType("活期储蓄存款");
                // 定期币种
                deposit.setCurrencyType("CNY");
                // 存期
                deposit.setPeriod(null);
                // 存期单位
                deposit.setPeriodUnit("");
                list.add(deposit)
            }
            creditCardAllData.setDeposits(list)
        }

    } catch (e) {
        logger.info("{}，解析定期活期的页面异常：{}", context.get(Constants.LUA_TASK_KEY), e);
        statCaughtExceptionByJS(context, e);
    }
}

    /**
     * 返回Referer值带有80820的httpost
     * @param
     * @return
     */
    function getHttpPostOne() {
        var header = new HashMap();
        header.put("User-Agent", urls.USER_AGENT);
        header.put("Accept", urls.ACCEPT);
        header.put("Host", "3g.cib.com.cn");
        header.put("Origin", "https://3g.cib.com.cn");
        header.put("Referer", "https://3g.cib.com.cn/app/80820.html");

        return header;
    }

    /**
     * 返回referer带有00002的httppost
     * @param
     * @return
     */
    function getHttpPostTwo() {

        var header = new HashMap();
        header.put("Referer", "https://3g.cib.com.cn/app/00002.html");
        header.put("User-Agent", urls.USER_AGENT);
        header.put("Origin", "https://3g.cib.com.cn");
        header.put("Host", "3g.cib.com.cn");
        header.put("Accept", urls.ACCEPT);
        return header;
    }

    /**
     * 返回referer带有10020的httppost
     * @param
     * @return
     */
    function getHttpPostThree() {
        var header = new HashMap();
        header.put("Referer", "https://3g.cib.com.cn/app/10020.html");
        header.put("User-Agent", urls.USER_AGENT);
        header.put("Origin", "https://3g.cib.com.cn");
        header.put("Host", "3g.cib.com.cn");
        header.put("Accept", urls.ACCEPT);
        return header;
    }

    /**
     * 返回referer带有10040的httppost
     * @param
     * @return
     */
    function getHttpPostFour() {

        var header = new HashMap();
        header.put("User-Agent", urls.USER_AGENT);
        header.put("Accept", urls.ACCEPT);
        header.put("Host", "3g.cib.com.cn");
        header.put("Origin", "https://3g.cib.com.cn");
        header.put("Referer", "https://3g.cib.com.cn/app/10040.html");
        return header;
    }


    /**
     * 判断字符串里面是否包含数字
     * @param description
     * @return
     */
    function isNumExists(description) {
        return description.matches(".*\\d+.*");
    }

    /**
     * 返回Referer值带有80820的httpost
     * @param
     * @return
     */
    function getHttpPostCreditOne() {
        var header = new HashMap();
        header.put("User-Agent", urls.USER_AGENT);
        header.put("Accept", urls.ACCEPT);
        header.put("Host", "3g.cib.com.cn");
        header.put("Origin", "https://3g.cib.com.cn");
        header.put("Referer", "https://3g.cib.com.cn/app/80820.html");
        return header;
    }

    /**
     * 返回Referer值带有10000的httpost
     * @param
     * @return
     */
    function getHttpPostCreditTwo() {

        var header = new HashMap();
        header.put("User-Agent", urls.USER_AGENT);
        header.put("Accept", urls.ACCEPT);
        header.put("Host", "3g.cib.com.cn");
        header.put("Origin", "https://3g.cib.com.cn");
        header.put("Referer", "https://3g.cib.com.cn/app/10000.html");
        return header;
    }

    /**
     * 返回Referer值带有50020的httpost
     * @param
     * @return
     */
    function getHttpPostCreditThree() {

        var header = new HashMap();
        header.put("User-Agent", urls.USER_AGENT);
        header.put("Accept", urls.ACCEPT);
        header.put("Host", "3g.cib.com.cn");
        header.put("Origin", "https://3g.cib.com.cn");
        header.put("Referer", "https://3g.cib.com.cn/app/50020.html");
        return header;
    }


    /**
     * 把分期描述里的数字提取出来或提取未出账单中的总页和当前页
     * @param des
     * @return
     */
    function getFen(des) {
        var list = new ArrayList();
        var r = Pattern.compile("\\d+");
        var m = r.matcher(des);
        while (m.find()) {
            list.add(Integer.parseInt(m.group()));
        }
        return list;
    }

    function processDate(date) {
        var s = "";
        if (date !== null) {
            s = date.trim().replaceAll("-", "");
            s = s.replaceAll("：", "");
            s = s.replaceAll("/", "");
            s = s.replaceAll("年", "");
            s = s.replaceAll("月", "");
            s = s.replaceAll("日", "");
            if (s.length() < 8) {
                s = s + "01";
            }
        }
        return s;
    }

// 根据中文币种生成英文币种
    function installmentTypeChange(type) {
        if (type.contains("账单")) {
            return "BILL";
        } else if (type.contains("现金")) {
            return "CASH";
        } else if (type.contains("消费") || type.contains("苹果") || type.contains("新兴业")) {
            return "CONSUME";
        } else {
            return "CONSUME";
        }
    }